package com.kede.app.rest;

import com.kede.annotation.Log;
import com.kede.app.domain.Virtual;
import com.kede.app.service.VirtualService;
import com.kede.app.service.dto.VirtualQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author Pr.li
* @date 2023-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "virtual管理")
@RequestMapping("/api/virtual")
public class VirtualController {

    private final VirtualService virtualService;

    /**
     * 导出数据
     **/
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('virtual:list')")
    public void exportVirtual(HttpServletResponse response, VirtualQueryCriteria criteria) throws IOException {
        virtualService.download(virtualService.queryAll(criteria), response);
    }

    /**
    * 查询virtual
    **/
    @GetMapping
    @Log("查询virtual")
    @ApiOperation("查询virtual")
    @PreAuthorize("@el.check('virtual:list')")
    public ResponseEntity<Object> queryVirtual(VirtualQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(virtualService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
    * 新增virtual
    **/
    @PostMapping
    @Log("新增virtual")
    @ApiOperation("新增virtual")
    @PreAuthorize("@el.check('virtual:add')")
    public ResponseEntity<Object> createVirtual(@Validated @RequestBody Virtual resources){
        return new ResponseEntity<>(virtualService.create(resources),HttpStatus.CREATED);
    }


    /**
    * 修改virtual
    **/
    @PutMapping
    @Log("修改virtual")
    @ApiOperation("修改virtual")
    @PreAuthorize("@el.check('virtual:edit')")
    public ResponseEntity<Object> updateVirtual(@Validated @RequestBody Virtual resources){
        virtualService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
    * 删除virtual
    **/
    @DeleteMapping
    @Log("删除virtual")
    @ApiOperation("删除virtual")
    @PreAuthorize("@el.check('virtual:del')")
    public ResponseEntity<Object> deleteVirtual(@RequestBody String[] ids) {
        virtualService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}